package org.uniview.common.persistence;

import java.sql.SQLException;

import com.ibatis.sqlmap.client.extensions.ParameterSetter;
import com.ibatis.sqlmap.client.extensions.ResultGetter;
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;

/**
 * 
 * @author James Gao
 * @version 1.0 2006-8-25
 */
public class CharBooleanHandler implements TypeHandlerCallback {

	/**
	 *
	 */
	public CharBooleanHandler() {
	}

	public void setParameter(ParameterSetter setter, Object parameter)
			throws SQLException {
		if (parameter == null) {
			setter.setString("");
		} else {
			boolean b = ((Boolean) parameter).booleanValue();
			if (b) {
				setter.setString("1");
			} else {
				setter.setString("0");
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 com.ibatis.sqlmap.client.extensions.TypeHandlerCallback#getResult(com.ib
	 atis.sqlmap.client.extensions.ResultGetter)
	 */
	public Object getResult(ResultGetter getter) throws SQLException {
		String s = getter.getString();
		if (s == null) {
			return null;
		} else if ("0".equalsIgnoreCase(s)) {
			return new Boolean(false);
		} else {
			return new Boolean(true);
		}
	}

	public Object valueOf(String s) {
		if (s == null) {
			return null;
		} else if ("1".equalsIgnoreCase(s)) {
			return new Boolean(true);
		} else {
			return new Boolean(false);
		}
	}

}
